package com.erp.module.duizhangPhoto.XxDuizhangPhoto

import android.content.Context
import com.erp.Interface.OnUserDataInterface
import com.erp.bean.DuizhangBean
import com.erp.bean.PhotoBean
import com.erp.module.duizhangPhoto.DuizhangPhotoContract
import com.erp.module.duizhangPhoto.DuizhangPhotoModel
import com.erp.util.JsonUtil
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

class IXxDuizhangPhoto(val KView: DuizhangPhotoContract.XxDuizhangPhotoView, val context: Context, var duizhangBean: DuizhangBean, var code: Int) : DuizhangPhotoContract.IXxDuizhangPhoto {
    var md = DuizhangPhotoModel()
    override fun getDuizhangPhoto(id:String) {
        md.getDuizhangPhoto(context,id,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
                json(data)
                KView.getDuizhangPhotoOk()
            }

            override fun onNot(throwable: String?) {
                KView.getDuizhangPhotoNo()
            }

        })
    }

    override fun addDuizhangPhoto(deletephoto: MutableList<PhotoBean>) {
        md.postDuizhangPhoto(context,duizhangBean,deletephoto,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
//                json(data)
                KView.addDuizhangPhotoOk()
            }

            override fun onNot(throwable: String?) {
                KView.addDuizhangPhotoNo()
            }

        })
    }

    fun json(data:String?){
        if (data==null){
            return
        }
        try {
            val j = JSONObject(data)
            val jo = JsonUtil.getjovolue(j,"data")
            duizhangBean.year = JsonUtil.getvolue(jo, "year")
            duizhangBean.month = JsonUtil.getvolue(jo, "month")
            duizhangBean.audit_time = JsonUtil.getvolue(jo, "audit_time")
            val js = JsonUtil.getjsvolue(jo,"photos")
            for (i in 0 until js.length()) {
                val b = PhotoBean()
                val joo = js.get(i) as JSONObject
                b.fid = JsonUtil.getvolue(joo, "fid")
                b.uri = JsonUtil.getvolue(joo, "uri")
                duizhangBean.listphoto.add(b)
            }
        } catch (e: JSONException) {
            e.printStackTrace()
        }
    }
}